package com.jike.noobmoney.mvp.view.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.jike.noobmoney.R;
import com.jike.noobmoney.adapter.MyTaskListAdapter;
import com.jike.noobmoney.contants.ConstantValue;
import com.jike.noobmoney.entity.MyTaskEntity;
import com.jike.noobmoney.mvp.presenter.TaskPresenter;
import com.jike.noobmoney.mvp.view.activity.SheshuSubmitTaskActivity;
import com.jike.noobmoney.mvp.view.activity.SubmitTaskActivity;
import com.jike.noobmoney.mvp.view.activity.UnFinishTaskActivity;
import com.jike.noobmoney.net.IView;
import com.jike.noobmoney.util.SPUtils;
import com.jike.noobmoney.util.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MyTaskFragment extends BaseNewFragment implements IView {

    public static String Channels = "channels";
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.empty)
    ImageView empty;

    private String status;
    private String userid;
    private MyTaskListAdapter adapter;
    private List<MyTaskEntity.OrderlistBean> mData = new ArrayList<>();
    private TaskPresenter taskPresenter;


    @Override
    protected int layoutResID() {
        return R.layout.fragment_my_task;
    }

    @Override
    protected void lazyLoad() {
        if (!isPrepared || !isVisible || mHasLoadedOnce) {
            return;
        }
        mHasLoadedOnce = true;
        taskPresenter = new TaskPresenter(this);
        initData();
    }

    protected void initData() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            status = bundle.getString(MyTaskFragment.Channels);
        }
        userid = SPUtils.getInstance().getString(ConstantValue.SpType.userid);

        getData();

        //adapter
        adapter = new MyTaskListAdapter(context);
        //manager
        recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(adapter);

        refreshLayout.setOnRefreshListener(refreshlayout -> {
            getData();
        });

        //item点击
        adapter.setOnItemClickListener((adapterView, view, position, l) -> {
            MyTaskEntity.OrderlistBean item = (MyTaskEntity.OrderlistBean) adapter.getItem(position);

            if (item.getType() == 3) {
                ToastUtils.showShortToastSafe("非系统任务,暂时无法查看");
                return;
            }
            switch (status) {
                case "1":
                    //未完成
                    Intent intent = new Intent(context, UnFinishTaskActivity.class);
                    intent.putExtra(UnFinishTaskActivity.t_id, "" + item.getT_id());
                    intent.putExtra(UnFinishTaskActivity.o_id, "" + item.getO_id());
                    startActivityForResult(intent, 100);
                    break;
                case "3":
                    //审核中
                    Intent intent3 = new Intent(context, SubmitTaskActivity.class);
                    intent3.putExtra(SubmitTaskActivity.o_id, "" + item.getO_id());
                    intent3.putExtra(SubmitTaskActivity.o_title, "审核中");
                    startActivity(intent3);
                    break;
                case "4":
                    //已完成
                    Intent intent4 = new Intent(context, SubmitTaskActivity.class);
                    intent4.putExtra(SubmitTaskActivity.o_id, "" + item.getO_id());
                    intent4.putExtra(SubmitTaskActivity.o_title, "已完成");
                    startActivity(intent4);

//                    Intent intent = new Intent(context, UnFinishTaskActivity.class);
//                    intent.putExtra(UnFinishTaskActivity.t_id, "" + "122081");
//                    intent.putExtra(UnFinishTaskActivity.o_id, "2265353" );
//                    startActivityForResult(intent, 100);

                    break;
                case "2":
                    //审核失败
                    Intent intent2 = new Intent(context, UnFinishTaskActivity.class);
                    intent2.putExtra(UnFinishTaskActivity.t_id, "" + item.getT_id());
                    intent2.putExtra(UnFinishTaskActivity.o_id, "" + item.getO_id());
                    startActivityForResult(intent2, 100);
                    break;
                case "5":
                    //被投诉
                    Intent intent5 = new Intent(context, SheshuSubmitTaskActivity.class);
                    intent5.putExtra(SheshuSubmitTaskActivity.o_id, "" + item.getO_id());
                    intent5.putExtra(SheshuSubmitTaskActivity.o_title, "被投诉");
                    startActivity(intent5);
                    break;
            }
        });

    }

    /**
     * 获取数据
     */
    private void getData() {
        showProgressDialog();
        if (taskPresenter != null) {
            taskPresenter.getMyTask(userid, status, ConstantValue.RequestKey.my_task);
        }

    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        disLoading();
        if (ConstantValue.RequestKey.my_task.equals(actionType)) {
            List<MyTaskEntity.OrderlistBean> orderlist = (List<MyTaskEntity.OrderlistBean>) data;
            if (orderlist != null && orderlist.size() > 0) {
                empty.setVisibility(View.GONE);
                mData.clear();
                mData.addAll(orderlist);
            } else {
                mData.clear();
                empty.setVisibility(View.VISIBLE);
            }
            adapter.refresh(mData);
        }
    }

    @Override
    public void onFailed(String code, String msg) {
        disLoading();
    }

    private void disLoading() {
        dismissProgressDialog();
        if (refreshLayout != null) {
            refreshLayout.finishRefresh();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == 200) {
            getData();
        }
    }


}
